<?php


namespace app\common\model;


use think\Db;

class Group extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_group';
    // 主键ID
    protected $pk = 'group_id';

    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = Db::table($this->table)
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    // 获取列表
    function get_list($cond = [])
    {
        $where = null;
        if(!empty($cond['group_not_ins'])){
            $not_in = is_array($cond['group_not_ins'])?implode(',',$cond['group_not_ins']):$cond['group_not_ins'];
            $where[] = ['group_id','not in',$not_in];
        }
        if(!empty($cond['group_ins'])){
            $ins = is_array($cond['group_ins'])?implode(',',$cond['group_ins']):$cond['group_ins'];
            $where[] = ['group_id','in',$ins];
        }
        $rs = Db::table($this->table)
            ->field('group_id,group_name')
            ->where($where)
            ->select();
        return $rs;
    }

    // 获取分页列表
    function get_list_page($cond,$orderby,$item,$limit)
    {
        $where = null;
        if(!empty($cond['group_name'])){
            $where[] = ['group_name','like',"%".$cond['group_name']."%"];
        }
        $rs = Db::table($this->table)
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);

        return $rs;
    }


    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['group_id' => $id];
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 更新数据
            $this->allowField(true)->save($params);
            $rs = $this->group_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

}